package it.kion.util.ui.umenumanager2;

import it.kion.util.ui.umenumanager2.epinterfaces.IMenuContributor;
import it.kion.util.ui.umenumanager2.model.Folder;
import it.kion.util.ui.umenumanager2.parser.DesktopMaker;
import it.kion.util.ui.umenumanager2.parser.MenuDOMParser;

import java.util.Collection;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class UMenuManager extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "it.kion.util.ui.umenumanager2";

	public static final String IMENU_CONTRIB = "it.kion.util.ui.umenumanager2.menu";

	// The shared instance
	private static UMenuManager plugin;
	
	private static MenuDOMParser PARSER;
	
	/**
	 * The constructor
	 */
	public UMenuManager() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		PARSER = new MenuDOMParser();
		new DesktopMaker();
		loadExtensions();
	}

	private void loadExtensions() {
		try {
			IConfigurationElement[] config = Platform.getExtensionRegistry()
					.getConfigurationElementsFor(IMENU_CONTRIB);
			for (IConfigurationElement e : config) {
				Object o = e.createExecutableExtension("class");
				if (o instanceof IMenuContributor) {
//					((IMenuContributor) o).greet();
				}
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in relative path
	 * 
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	public static Collection<Folder> getMainFolders() {
		return PARSER.getFolders();
	}

	public static MenuDOMParser getParser() {
		return PARSER;
	}

	public static void openResource(String resourcePath) {
		IPath resourceIPath = new Path ( resourcePath );
		IEditorDescriptor[] editorDescripotrs = PlatformUI.getWorkbench().getEditorRegistry().getEditors(resourcePath);
		if(editorDescripotrs!=null && editorDescripotrs.length>0){
			IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
			IWorkbenchPage page = window.getActivePage();
			IEditorDescriptor editor = editorDescripotrs[0];
			String editorId = editor.getId();
			IFile input = ResourcesPlugin.getWorkspace().getRoot().getFile(resourceIPath);
			try {
				IDE.openEditor(page, input, editorId);
			}
			catch (PartInitException e) {
				MessageDialog.openInformation(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "MenuManagerView", "Errore");
				e.printStackTrace();
			}
			
		}
	}

	static public void setParser(MenuDOMParser parser){
		PARSER = parser;
	}
	
	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static UMenuManager getDefault() {
		return plugin;
	}

}
